package com.main;
/**
 * @author Piazza Francesco Giovanni ,Tecnes Milano http://www.tecnes.com
 *
 */
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.Date;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.RepaintManager;

import com.CubicMesh;
import com.DrawObject;
import com.LineData;
import com.Plain;
import com.Point3D;
import com.Point4D;
import com.Polygon3D;
import com.PolygonMesh;
import com.Texture;
import com.ZBuffer;







/*
 * Created on 12/apr/08
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

public class Road extends Renderer3D{

	public static final int Y_STEP = 10;
	int y_edge=5;
	int NX=2;
	int NY=80;

	
	int NXVISIBLE=NX;
	int NYVISIBLE=20;//20 orig

	int dx=0;
	int dy=3000/NYVISIBLE;//600 orig

	int ROAD_LENGHT=600;

	int CAR_WIDTH=100;
	
	public static int FORWARD=1;
	
	public static final int EXTERNAL_CAMERA=0;
	public static final int DRIVER_CAMERA=1;
	public static int CAMERA_TYPE=EXTERNAL_CAMERA;
	

	
	int TILE_SIDE=4;
	CarData carData=null;
	
   
	//public static String[] hexRoadColors={"888888","888888","888888","CCCCCC"};

	Point4D[][] roadData=new Point4D[NY][NX];
	DrawObject[] drawObjects=null;

	Point4D [][] origRoadData=null;
	DrawObject[]  origDrawObjects=null;
	

	private JFileChooser fc;
	boolean start=true;
	

	
	private int APOSX;


	public static double turningAngle=0;
	public static double dTurningAngle=0.1;
	public static boolean steer=false;

	
	int whiteRGB=Color.WHITE.getRGB();
	
	int SELECTED_CAR=0;

	private Texture carTexture=null;
	private Date time;
	private Area totalVisibleField=null;

		 
	public Road(){}

	public Road(int WITDH,int HEIGHT){

		dx=WITDH/(NXVISIBLE-1);
		//dy=HEIGHT/(NYVISIBLE-1);

		this.HEIGHT=HEIGHT;
		this.WIDTH=WITDH;
		YFOCUS=HEIGHT/2;
		XFOCUS=WITDH/2;
		loadRoad();
		
		totalVisibleField=buildVisibileArea(0,NYVISIBLE*dy);
		
		
	}

	//road with white border to delete
	public void loadRoad() {

		observerPoint=new Point3D(XFOCUS,-SCREEN_DISTANCE,YFOCUS);
		lightPoint=new Point3D(XFOCUS,-SCREEN_DISTANCE,YFOCUS);
		
		loadRoadFromFile(new File("lib/road_default"));
		
		loadObjectsFromFile(new File("lib/objects_default"));
		
		roadZbuffer=new ZBuffer[WIDTH*HEIGHT];
	
		buildNewZBuffer();
	
		
		
		//showMemoryUsage();
		//buildDefaultRoad();
		

	}
	
	private void buildDefaultRoad() {
		
		
		
		NX=26;
		NY=21;

		 roadData=new Point4D[NY][NX]; 
		 for(int j=0;j<NY;j++){

			for(int i=0;i<NX;i++){

				int delx=-400;
				int delz=(int)(100*Math.cos(Math.PI*j/12));
				//int delz=(int)(100*Math.cos(Math.PI*j/12)*Math.sin(Math.PI*i/12));
				roadData[j][i].x=dx*i+delx;
				roadData[j][i].y=dy*j;
				roadData[j][i].z=delz;


			}
		}
		saveRoad();
		
	}

	private void loadCar(int k) {


			carData=new CarData();

			loadCarFromFile(new File("lib/cardefault3D_"+k),k);

	
			
		

	}
	
	public void initCar(){
		
			initCar(0);
		
		
	}
	
	public void selectNextCar() {
		
		SELECTED_CAR+=1;
		if(!(new File("lib/cardefault3D_"+SELECTED_CAR)).exists())
			SELECTED_CAR=0;
	
			initCar(SELECTED_CAR);
		
		
	}

	public void initCar(int k) {

		loadCar(k);
		carTexture =null;
		
		try {
			carTexture=new Texture(ImageIO.read(new File("lib/car_texture_t"+k+".gif")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		/*carZbuffer=new ZBuffer[WIDTH*HEIGHT];
		for(int i=0;i<WIDTH*HEIGHT;i++){


			carZbuffer[i]=new ZBuffer(greenRgb,0);

		}
		buildCar(0);
	    */
	}

	public void buildCar(double directionAngle){
		
	


				
		Point3D xDirection=null;
		Point3D yDirection=null;

		
		Point3D steeringCenter=new Point3D(WIDTH/2, y_edge,0);
		Point3D leftCorner=new Point3D(WIDTH/2-CAR_WIDTH/2, y_edge,0);
		
		
		Point3D zVersor=new Point3D(0,0,1);
		Point3D zMinusVersor=new Point3D(0,0,-1);
	
		
		Point3D xVersor=new Point3D(Math.cos(directionAngle),Math.sin(directionAngle),0);
		Point3D yVersor=new Point3D(-Math.sin(directionAngle),Math.cos(directionAngle),0);
		
		Point3D rotateLeftUpperCorner=leftCorner.clone();
		rotateLeftUpperCorner=rotateLeftUpperCorner.buildTranslatedPoint(0,0,carData.getDeltaX());
		rotateLeftUpperCorner.rotate(steeringCenter.x,steeringCenter.y,Math.cos(directionAngle),Math.sin(directionAngle));

		Point3D rotateLeftCorner=leftCorner.clone();
		rotateLeftCorner.rotate(steeringCenter.x,steeringCenter.y,Math.cos(directionAngle),Math.sin(directionAngle));

	   PolygonMesh pm=carData.getCarMesh();
	  
		
		for(int i=0;i<pm.polygonData.size();i++){
			
			LineData ld=pm.polygonData.elementAt(i);
			Point3D normal= pm.normals.elementAt(i);
			
			int deltaWidth=0;
			int deltaHeight=carData.getDeltaY();
			
			int due=(int)(255-i*15);
			Color col=new Color(due,0,0);
			
			Polygon3D polRotate=PolygonMesh.getBodyPolygon(carData.getCarMesh().points,ld);
			int face=findBoxFace(normal);
			rotatePolygon(polRotate,(int)steeringCenter.x,(int)steeringCenter.y,Math.cos(directionAngle),Math.sin(directionAngle));
		
			
			Point3D rotateOrigin=rotateLeftCorner;
            //System.out.println(i+" "+face);
			if(face==CAR_BOTTOM || face==CAR_FRONT)
				continue;
		
			if(face==CAR_BACK){
				 deltaWidth=carData.getDeltaX();
				 deltaHeight=0;
				 xDirection=xVersor;
				 yDirection=zVersor;


			}
			else if(face==CAR_TOP){
				 deltaWidth=carData.getDeltaX();
				 xDirection=xVersor;
				 yDirection=yVersor;


			}
			else if(face==CAR_LEFT) {
				
				xDirection=zVersor;
				yDirection=yVersor;
				

	
	
			}
			else if(face==CAR_RIGHT) {
				
				xDirection=zMinusVersor;
				yDirection=yVersor;
				rotateOrigin=rotateLeftUpperCorner;

				deltaWidth=carData.getDeltaX2();
			}
			
			decomposeClippedPolygonIntoZBuffer(polRotate,col,carTexture,roadZbuffer,xDirection,yDirection,rotateOrigin,deltaWidth,deltaHeight);
				
		}
		
	
	}


	


	public void drawRoad(BufferedImage buf){

		
		//cleanZBuffer();
		
		drawSky();
		drawObjects(drawObjects,totalVisibleField,roadZbuffer);
		
		MOVZ=0;
		boolean firstValue=true;

		int startX=WIDTH/2;
		int startY=5;
		
		int PARTIAL_MOVZ=0;
	
		for(int j=0;j<NY-1;j++)	{

			int lenght=roadData[j].length;
			
			for(int i=0;i<lenght-1;i++)	{

				Polygon3D p3D=buildTranslatedPolygon3D(i,roadData[j],roadData[j+1]);

				if(p3D.contains(startX,startY)){

					PARTIAL_MOVZ=-(int)interpolate(startX,startY,p3D);
					//exit cycle
					j=NY-1;
					break;

				}


			}


		}
        
		MOVZ=PARTIAL_MOVZ;
		
		for(int j=0;j<NY-1;j++)	{
			
			int lenght=roadData[j].length;

			for(int i=0;i<lenght-1;i++){
				//if(j>0 || i>0) continue;
				Polygon3D p3D=buildTranslatedPolygon3D(i,roadData[j],roadData[j+1]);
				
				if(!p3D.clipPolygonToArea2D(totalVisibleField).isEmpty())
			          decomposeClippedPolygonIntoZBuffer(p3D,ZBuffer.fromHexToColor(p3D.getHexColor()),CarFrame.worldTextures[p3D.getIndex()],roadZbuffer);

			}
		}
		


		
		drawCar();
		
		
		buildScreen(buf); 
		//showMemoryUsage();
		
	}

	private void drawCar() {
	
		if(VIEW_TYPE==REAR_VIEW || CAMERA_TYPE==DRIVER_CAMERA)
			return;
		if(!steer){

			buildCar(0);
		
		}
		else {
		
			if(turningAngle==0){
				
				
				return;
			}
			else
				buildCar(-dTurningAngle*Math.signum(turningAngle));
		
		} 

		if(turningAngle!=0 && !steer )
		{   turningAngle=0;
			buildCar(0);
			CarFrame.setSteeringAngle();
		    
		}
		
	}



	private Area buildVisibileArea(int y1, int y2) {
		int[] cx=new int[4];
		int[] cy=new int[4];



		cx[0]=limVisibileL(y1);
		cy[0]=y1;
		cx[1]=limVisibileR(y1);
		cy[1]=y1;
		cx[2]=limVisibileR(y2);
		cy[2]=y2;
		cx[3]=limVisibileL(y2);
		cy[3]=y2;

		Polygon p=new Polygon(cx,cy,4);

		Area a = new Area(p);

		//System.out.println(Polygon3D.fromAreaToPolygon2D(a));

		return a;
	}

	private int limVisibileL(double y1) {

		return (int) (XFOCUS+(y1+SCREEN_DISTANCE)*(-XFOCUS)/SCREEN_DISTANCE);
	}

	private int limVisibileR(double y1) {

		return (int) (XFOCUS+(y1+SCREEN_DISTANCE)*(WIDTH-XFOCUS)/SCREEN_DISTANCE);
	}



	public Polygon3D buildTranslatedPolygon3D(int i,Point4D[] is,  Point4D[] is2){

		int[] cx=new int[4];
		int[] cy=new int[4];
		int[] cz=new int[4];	

		if(VIEW_TYPE==FRONT_VIEW ){
			cx[0]=(int) is[i].x-POSX;
			cy[0]=(int) is[i].y-POSY;
			cz[0]=(int) is[i].z+MOVZ;
			cx[1]=(int) is[i+1].x-POSX;
			cy[1]=(int) is[i+1].y-POSY;
			cz[1]=(int) is[i+1].z+MOVZ;
			cx[2]=(int) is2[i+1].x-POSX;
			cy[2]=(int) is2[i+1].y-POSY;
			cz[2]=(int) is2[i+1].z+MOVZ;
			cx[3]=(int) is2[i].x-POSX;
			cy[3]=(int) is2[i].y-POSY;
			cz[3]=(int) is2[i].z+MOVZ;
		}
		else{
			
			cx[0]=2*XFOCUS-((int) is[i].x-POSX);
			cy[0]=-((int) is[i].y-POSY);
			cz[0]=(int) is[i].z+MOVZ;
			cx[1]=2*XFOCUS-((int) is[i+1].x-POSX);
			cy[1]=-((int) is[i+1].y-POSY);
			cz[1]=(int) is[i+1].z+MOVZ;
			cx[2]=2*XFOCUS-((int) is2[i+1].x-POSX);
			cy[2]=-((int) is2[i+1].y-POSY);
			cz[2]=(int) is2[i+1].z+MOVZ;
			cx[3]=2*XFOCUS-((int) is2[i].x-POSX);
			cy[3]=-((int) is2[i].y-POSY);
			cz[3]=(int) is2[i].z+MOVZ;
			
			
		}
		Polygon3D p3D=new Polygon3D(4,cx,cy,cz);

		p3D.setHexColor(is[i].getHexColor());
		p3D.setIndex(is[i].getIndex());
		
		return p3D;
	}

	private boolean isVisible(Rectangle rect, Polygon3D translatedPolygon) {
		
		int maxY=rect.y+rect.height;
		
		for(int i=0;i<translatedPolygon.npoints;i++){
			
			if(translatedPolygon.ypoints[0]<maxY && translatedPolygon.ypoints[0]>rect.y)
				return true;
		}
		
		return false;
	}

	public void rotate(){
		
		if(turningAngle==0){
			
			
			return;
		}

		APOSX+=(int) (-turningAngle/(Math.PI*2)*CarFrame.background.getWidth());
		double xo=POSX;
		if(turningAngle*FORWARD>0) xo=POSX+WIDTH;
		double yo=POSY+y_edge;
		
		if(CAMERA_TYPE==DRIVER_CAMERA){
			
			yo=POSY-SCREEN_DISTANCE;
		}
		
		double ct=Math.cos(turningAngle);
		double st=Math.sin(turningAngle);

		for(int j=0;j<NY;j++)	{

			for(int i=0;i<roadData[j].length;i++){



				double x=roadData[j][i].x;
				double y=roadData[j][i].y;
				

				roadData[j][i].x=xo+(x-xo)*ct-(y-yo)*st;
				roadData[j][i].y=yo+(y-yo)*ct+(x-xo)*st;
			}


		}

		for(int i=0;i<drawObjects.length;i++){

			DrawObject dro=drawObjects[i];

			double x=dro.x;
			double y=dro.y;	

			dro.x=xo+(x-xo)*ct-(y-yo)*st;
			dro.y=yo+(y-yo)*ct+(x-xo)*st;

			rotatePolygon(dro,xo,yo,ct,st);
			rotateMesh((CubicMesh) dro.getMesh(),xo,yo,ct,st);

		}
		
		double obx=lightPoint.x;
		double oby=lightPoint.y;
		

		lightPoint.x=xo+(obx-xo)*ct-(oby-yo)*st;
		lightPoint.y=yo+(oby-yo)*ct+(obx-xo)*st;
		
		
		    
	}



	public void rotateSky(double rotationAngle){
		APOSX+=(int) (-rotationAngle/(Math.PI*2)*CarFrame.background.getWidth());
	}
	
	public void drawSky() {

		//g2.setColor(Color.BLUE);
		//g2.fillRect(0,0,WIDTH,HEIGHT-YFOCUS);

		int dw=CarFrame.background.getWidth();
		int dh=CarFrame.HEIGHT-YFOCUS;
		
		int height=CarFrame.background.getHeight();
		
		//System.out.println(APOSX);
		for(int i=0;i<dw;i++){

			int i_set=(i+APOSX)%dw;
			if(i_set<0) i_set=i_set+dw;

			if(i_set>=CarFrame.WIDTH)
				continue;
			
			
			
			for(int j=height-dh;j<height;j++){

				int tot=(dh+j-height)*HEIGHT+i_set;
				
				
					
				int rgb=CarFrame.background.getRGB(i,j);
				roadZbuffer[tot].setRgbColor(rgb);
				

			}
		}	

	}

	public void reset(Graphics2D g2) {
		
		CarFrame.CAR_SPEED=0;
		g2.setColor(CarFrame.BACKGROUND_COLOR);
		g2.fillRect(0,YFOCUS,WIDTH,HEIGHT-YFOCUS);
		Road.turningAngle=0;
		steer=false;
		POSX=0;
		POSY=0;
		APOSX=0;
		
		//loadRoad();
		
		try {
			Thread.sleep(250);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		roadData=cloneRoadData(origRoadData);
		drawObjects=DrawObject.cloneObjectsArray(origDrawObjects);
		
		lightPoint=new Point3D(XFOCUS,-SCREEN_DISTANCE,YFOCUS);
	}

	public void up(Graphics2D graphics2D) {

        
		if(!steer){

			
			POSY=(int) (POSY+FORWARD*CarFrame.CAR_SPEED*Y_STEP);
           
			//debug
			//if(POSY>4000)
           	//System.out.println("1-"+(System.currentTimeMillis()-time.getTime()));
		}
		else {
			rotate();
	
		} 


	}
	public void start(){
		//for debug
		//time=new Date();

	}
	

	public void down(Graphics2D graphics2D) {

		if(POSY>0)
			POSY=POSY-dy;


	}


	private void saveRoad() {

		fc = new JFileChooser();
		fc.setDialogType(JFileChooser.SAVE_DIALOG);
		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			saveRoad(file);

		} 


	}

	public void saveRoad(File file) {



		PrintWriter pr;
		try {
			pr = new PrintWriter(new FileOutputStream(file));
			pr.println("#NX="+NX);
			pr.println("#NY="+NY);

			for(int j=0;j<NY;j++){



				pr.print(decomposeRow(roadData[j]));


				pr.println();
			}	
			pr.close(); 	

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
	}

	private String decomposeRow(Point4D[] is) {
		String str="";

		for(int i=0;i<is.length;i++){

			if(i>0) str+="_";
			str+=is[i].x+","+is[i].y+","+is[i].z;
		}

		return str;
	}
	
	public void loadRoadFromFile(){	

		fc=new JFileChooser();
		fc.setDialogType(JFileChooser.OPEN_DIALOG);

		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			loadRoadFromFile(file);


		}
	}

	public void loadRoadFromFile(File file){

		try {
			BufferedReader br=new BufferedReader(new FileReader(file));

			String snx=br.readLine();
			String sny=br.readLine();

			if(snx==null || sny==null) {

				br.close();
				return;
			}

			NX=Integer.parseInt(snx.substring(4));
			NY=Integer.parseInt(sny.substring(4));
			roadData=new Point4D[NY][NX];

			String str=null;
			int rows=0;
			while((str=br.readLine())!=null){

				roadData[rows]=buildRow(str);

				rows++;	

			}

			br.close();

			origRoadData=cloneRoadData(roadData);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	private Point4D[][] cloneRoadData(Point4D[][] roadData2) {

		Point4D[][] newRoadData=new Point4D[NY][NX];


		for(int j=0;j<NY;j++)
			for(int i=0;i<NX;i++)
				for(int k=0;k<3;k++)
					newRoadData[j][i]=(Point4D) roadData2[j][i].clone();

		return newRoadData;
	}

	public void loadObjectsFromFile(){	

		fc=new JFileChooser();
		fc.setDialogType(JFileChooser.OPEN_DIALOG);

		int returnVal = fc.showOpenDialog(null);

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			loadObjectsFromFile(file);


		}
	}

	public void loadCarFromFile(File file, int k) {
		


		Vector <Point3D>points = new Vector <Point3D>();
		Vector <LineData>lines = new Vector <LineData>();
		

		try {
			BufferedReader br=new BufferedReader(new FileReader(file));


			String str=null;

			while((str=br.readLine())!=null){
				if(str.indexOf("#")>=0 || str.length()==0)
					continue;

				if(str.startsWith("P="))
					PolygonMesh.buildPoints(points,str.substring(2));
				else if(str.startsWith("L="))
					PolygonMesh.buildLines(lines,str.substring(2));


			}
		    carData.setCarMesh(points,lines);
		    
	        //find maxs
			for(int j=0;j<points.size();j++){
				
				Point3D point= points.elementAt(j);
				carData.deltaX2=(int)Math.max(point.x,carData.deltaX2);
				carData.deltaX=(int)Math.max(point.z,carData.deltaX);
				carData.deltaY=(int)Math.max(point.z,carData.deltaY);
				carData.deltaY2=(int)Math.max(point.y,carData.deltaY2);
			}
	
			carData.deltaX2=+carData.deltaX2+carData.deltaX+1;
			carData.deltaY=carData.deltaY+1;
			carData.deltaY2=carData.deltaY2+carData.deltaY;
			carData.deltaX=carData.deltaX+1;
			
			CAR_WIDTH=carData.deltaX2-carData.deltaY;
			carData.getCarMesh().translate(WIDTH/2-CAR_WIDTH/2,y_edge,0);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public void loadObjectsFromFile(File file){
		
		Vector vdrawObjects=new Vector();
		
	
	
		try {
			BufferedReader br=new BufferedReader(new FileReader(file));


			String str=null;
			int rows=0;
			while((str=br.readLine())!=null ){
				if(str.indexOf("#")>=0 || str.length()==0)
					continue;
				DrawObject dro=buildDrawObject(str);
				
				buildRectanglePolygons(dro.getPolygons(),dro.x,dro.y,dro.z,dro.dx,dro.dy,dro.dz);
								
				vdrawObjects.add(dro);
				
				CubicMesh cm=(CubicMesh) CarFrame.object3D[dro.getIndex()].clone();
				
				Point3D point = cm.point000;
				
				double dx=-point.x+dro.x;
				double dy=-point.y+dro.y;
				double dz=-point.z+dro.z;
				
				cm.translate(dx,dy,dz);
				
				dro.setMesh(cm);
				

			}

			br.close();
			
			drawObjects=new DrawObject[vdrawObjects.size()];
			
			
			for (int i = 0; i < vdrawObjects.size(); i++) {
				drawObjects[i]=(DrawObject) vdrawObjects.elementAt(i);
			}
			
			origDrawObjects=DrawObject.cloneObjectsArray(drawObjects);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}



	private DrawObject buildDrawObject(String str) {
		DrawObject dro=new DrawObject();

		StringTokenizer tok=new StringTokenizer(str,"_");
		dro.x=Double.parseDouble(tok.nextToken());
		dro.y=Double.parseDouble(tok.nextToken());
		dro.z=Double.parseDouble(tok.nextToken());
		dro.dx=Double.parseDouble(tok.nextToken());
		dro.dy=Double.parseDouble(tok.nextToken());
		dro.dz=Double.parseDouble(tok.nextToken());
		dro.index=Integer.parseInt(tok.nextToken());
		dro.hexColor=tok.nextToken();
		return dro;
	}

	private Point4D[] buildRow(String string) {
		StringTokenizer stk=new StringTokenizer(string,"_");

		Point4D[] row = new Point4D[NX];
		int columns=0;
		while(stk.hasMoreTokens()){

			String[] vals=stk.nextToken().split(",");

			row[columns]=new Point4D();

			row[columns].x=Double.parseDouble(vals[0]);
			row[columns].y=Double.parseDouble(vals[1]);
			row[columns].z=Double.parseDouble(vals[2]);
			row[columns].setHexColor(vals[3]);
			row[columns].setIndex(Integer.parseInt(vals[4]));
			columns++;
		}

		return row;
	}

	public static String decomposeColor(Color tcc) {
		return addZeros(tcc.getRed())+","+addZeros(tcc.getGreen())+","+addZeros(tcc.getBlue());

	}

	public static String addZeros(int numb){
		String newStr=""+numb;
		newStr="00"+newStr;
		newStr=newStr.substring(newStr.length()-3,newStr.length());

		return newStr;

	}

	public static Color buildColor(String colorString) {

		if(colorString==null) return null;
		Color tcc=null;
		String[] colorComponents = colorString.split(",");
		tcc=new Color(Integer.parseInt(colorComponents[0]),Integer.parseInt(colorComponents[1]),Integer.parseInt(colorComponents[2]));
		return tcc;

	}




	/*public static void main(String[] args) {
		Road road=new Road(500,600);
		//road.saveRoad();
		//road.loadRoadFromFile();
		road.loadRoad();

	}*/





	public void changeCamera(int camera_type) {
		
		if(camera_type==EXTERNAL_CAMERA){
			
			CAMERA_TYPE=EXTERNAL_CAMERA;
			POSY=POSY-(int) SCREEN_DISTANCE;
			YFOCUS=HEIGHT/2;
		}
		else if(camera_type==DRIVER_CAMERA){
			
			CAMERA_TYPE=DRIVER_CAMERA;
			POSY= POSY+(int)SCREEN_DISTANCE;
			YFOCUS=HEIGHT/5;
			
		}	
	
		
	}


	

	

}
